package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.ReportLjlmPzdsmzcBaDetailBo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmPzdsmzcBaDetailVo;
import com.ruoyi.report_ljlm.domain.ReportLjlmPzdsmzcBaDetail;
import com.ruoyi.report_ljlm.mapper.ReportLjlmPzdsmzcBaDetailMapper;
import com.ruoyi.report_ljlm.service.IReportLjlmPzdsmzcBaDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面平整度试验检测记录（三米直尺法)记录详细Service业务层处理
 *
 * @author zyb
 * @date 2024-10-29
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportLjlmPzdsmzcBaDetailServiceImpl implements IReportLjlmPzdsmzcBaDetailService {

    private final ReportLjlmPzdsmzcBaDetailMapper baseMapper;

    /**
     * 查询路基路面平整度试验检测记录（三米直尺法)记录详细
     */
    @Override
    public ReportLjlmPzdsmzcBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询路基路面平整度试验检测记录（三米直尺法)记录详细列表
     */
    @Override
    public TableDataInfo<ReportLjlmPzdsmzcBaDetailVo> queryPageList(ReportLjlmPzdsmzcBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLjlmPzdsmzcBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLjlmPzdsmzcBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面平整度试验检测记录（三米直尺法)记录详细列表
     */
    @Override
    public List<ReportLjlmPzdsmzcBaDetailVo> queryList(ReportLjlmPzdsmzcBaDetailBo bo) {
        LambdaQueryWrapper<ReportLjlmPzdsmzcBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLjlmPzdsmzcBaDetail> buildQueryWrapper(ReportLjlmPzdsmzcBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLjlmPzdsmzcBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLjlmPzdsmzcBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBaRowNumber() != null, ReportLjlmPzdsmzcBaDetail::getBaRowNumber, bo.getBaRowNumber());
        lqw.eq(bo.getHehId() != null, ReportLjlmPzdsmzcBaDetail::getHehId, bo.getHehId());
        lqw.eq(bo.getXuh() != null, ReportLjlmPzdsmzcBaDetail::getXuh, bo.getXuh());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuanghjwz()), ReportLjlmPzdsmzcBaDetail::getZhuanghjwz, bo.getZhuanghjwz());
        lqw.eq(bo.getShicz1() != null, ReportLjlmPzdsmzcBaDetail::getShicz1, bo.getShicz1());
        lqw.eq(bo.getShicz2() != null, ReportLjlmPzdsmzcBaDetail::getShicz2, bo.getShicz2());
        lqw.eq(bo.getShicz3() != null, ReportLjlmPzdsmzcBaDetail::getShicz3, bo.getShicz3());
        lqw.eq(bo.getShicz4() != null, ReportLjlmPzdsmzcBaDetail::getShicz4, bo.getShicz4());
        lqw.eq(bo.getShicz5() != null, ReportLjlmPzdsmzcBaDetail::getShicz5, bo.getShicz5());
        lqw.eq(bo.getShicz6() != null, ReportLjlmPzdsmzcBaDetail::getShicz6, bo.getShicz6());
        lqw.eq(bo.getShicz7() != null, ReportLjlmPzdsmzcBaDetail::getShicz7, bo.getShicz7());
        lqw.eq(bo.getShicz8() != null, ReportLjlmPzdsmzcBaDetail::getShicz8, bo.getShicz8());
        lqw.eq(bo.getShicz9() != null, ReportLjlmPzdsmzcBaDetail::getShicz9, bo.getShicz9());
        lqw.eq(bo.getShicz10() != null, ReportLjlmPzdsmzcBaDetail::getShicz10, bo.getShicz10());
        lqw.eq(bo.getPingjz() != null, ReportLjlmPzdsmzcBaDetail::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBuhgcs() != null, ReportLjlmPzdsmzcBaDetail::getBuhgcs, bo.getBuhgcs());
        lqw.eq(bo.getHegl() != null, ReportLjlmPzdsmzcBaDetail::getHegl, bo.getHegl());
        return lqw;
    }

    /**
     * 新增路基路面平整度试验检测记录（三米直尺法)记录详细
     */
    @Override
    public Boolean insertByBo(ReportLjlmPzdsmzcBaDetailBo bo) {
        ReportLjlmPzdsmzcBaDetail add = BeanUtil.toBean(bo, ReportLjlmPzdsmzcBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改路基路面平整度试验检测记录（三米直尺法)记录详细
     */
    @Override
    public Boolean updateByBo(ReportLjlmPzdsmzcBaDetailBo bo) {
        ReportLjlmPzdsmzcBaDetail update = BeanUtil.toBean(bo, ReportLjlmPzdsmzcBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLjlmPzdsmzcBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面平整度试验检测记录（三米直尺法)记录详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
